﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Testajaxfileupload.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="js/jquery-1.9.1.js"></script>
    <script src="js/ajaxfileupload.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnUpload").click(function () {
                ajaxFileUpload();
            })
        })


        function ajaxFileUpload() {
            $.ajaxFileUpload({
                url: '/upload.aspx', //用于文件上传的服务器端请求地址
                secureuri: false, //是否需要安全协议，一般设置为false
                fileElementId: 'file1', //文件上传域的ID
                dataType: 'json', //返回值类型 一般设置为json
                success: function (data, status)  //服务器成功响应处理函数
                {
                    $("#img1").attr("src", data.imgurl);
                    if (typeof (data.error) != 'undefined') {
                        if (data.error == '') {
                            //alert(data.msg);
                            alert("上传成功");
                        } else {
                            alert(data.error);
                        }
                    }
                },
                error: function (data, status, e)//服务器响应失败处理函数
                {
                    alert(e);
                }
            })
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <p>
            <input type="file" id="file1" name="file" />
        </p>
        <input type="button" id="btnUpload" value="上传" />
        <p>
            <img id="img1" src="" />
        </p>
    </form>
</body>
</html>
